///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_ZINTEGER_H_
#define _BUOLA_ZZ_ZINTEGER_H_

#include <buola/zz/zbase.h>

namespace buola { namespace zz {

template<typename tInt,unsigned vRadix=10>
class ZInteger : public ZBase<ZInteger<tInt,vRadix>>
{
public:
    template<typename tIt>
    struct tAttribute
    {
        typedef tInt tType;
    };

    ZInteger()
    {}
    
    ZInteger(const ZInteger&)=default;
    ZInteger(ZInteger&&)=default;
    
    static_assert(vRadix>0&&vRadix<=16,"not supported radix");
    
    template<typename tIt,typename tAttr,typename tCtx,typename tSk>
    bool Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx &pCtx,const tSk &pSk) const;
};

template<typename tInt,unsigned vRadix>
template<typename tIt,typename tAttr,typename tCtx,typename tSk>
bool ZInteger<tInt,vRadix>::Parse(tIt &pB,const tIt &pE,tAttr &pAttr,tCtx&,const tSk &pSk) const
{
    skip_over(pB,pE,pSk);

    if(pB==pE) return false;
    tIt lI=pB;
    
    tInt lSign=1;
    tInt lResult=0;
    switch(*lI)
    {
    case '-':
        lSign=-1;
    case '+':
        ++lI;
        if(lI==pE) return false;
        break;
    default:
        break;
    }
    
    switch(*lI)
    {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        lResult=*lI-'0';
        ++lI;
        break;
    default:
        return false;
    }
    
    while(lI!=pE)
    {
        switch(*lI)
        {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            lResult*=vRadix;
            lResult+=*lI-'0';
            ++lI;
            break;
        default:
            pB=lI;
            pAttr=lResult*lSign;
            return true;
        }
    }
    
    pB=lI;
    pAttr=lResult*lSign;
    return true;
}

inline ZInteger<int32_t> int32()
{
    return {};
}

/*namespace zz*/ } /*namespace buola*/ }

#endif
